package com.htjc.integral.account.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.htjc.integral.account.util.enums.NewAccountConstant;
import com.sgcc.sip.access.rest.SipRestAccessAgent;
import com.sgcc.sip.access.rest.SipRestAccessAgentFactory;

import lombok.extern.slf4j.Slf4j;

/**
 * 电e宝统一用户中心
 * @author hlc
 *
 */
@Slf4j(topic = "jfLog")
@Component
public class DebUserCentreUtil {
	
	public static String account;
	//sip接入密码，sip平台配置管理
	public static String pwd;
	//sip加密因子，sip平台配置管理
	public static String secretKey;
	//sip服务地址
	public static String uscBaseUrl;
	//sip member
	public static String member;
	
	@Value("${sip.account}")
	public void setAccount(String account1) {
		account = account1;
	}
	
	@Value("${sip.pwd}")
	public void setPwd(String pwd1) {
		pwd = pwd1;
	}
	
	@Value("${sip.secretKey}")
	public void setSecretKey(String secretKey1) {
		secretKey = secretKey1;
	}

	@Value("${sip.uscBaseUrl}")
	public void setUscBaseUrl(String uscBaseUrl1) {
		uscBaseUrl = uscBaseUrl1;
	}
	
	@Value("${sip.member}")
	public void setMember(String member1) {
		member = member1;
	}

	public static SipRestAccessAgentFactory factory=null;
	
	public static JSONObject queryConsNoList(String accountId,String token,String logMsg){
		if(factory==null){
			factory=new SipRestAccessAgentFactory(uscBaseUrl+"/sip/authz/gettoken", account, pwd, secretKey, "111111");
		}
		JSONObject resultJson=new JSONObject();
		ObjectMapper objectMapper = new ObjectMapper();
		SipRestAccessAgent agent = factory.getInstance();
		//登录认证
		Map<String, String> map;
		try {
			map = new HashMap<String, String>();
			map.put("tenant", "state_grid");
			map.put("member", member);
			String uscInfo = objectMapper.writeValueAsString(map);
			map.clear();
			map.put("userId", accountId);
			String quInfo = objectMapper.writeValueAsString(map);
			map.clear();
			map.put("uscInfo", uscInfo);
			map.put("quInfo", quInfo);
			map.put("token", token);
		} catch (Exception e) {
			log.error(logMsg+"调用统一用户中心请求参数处理异常",e);
			resultJson.put("returnCode", NewAccountConstant.RETURN_CODE_FAIL);
			resultJson.put("returnMsg", "调用统一用户中心请求参数处理异常");
			return resultJson;
		}
		String info=null;
		try {
			log.info(logMsg+"调用统一用户中心请求参数："+JSONObject.toJSONString(map));
			info = agent.post(uscBaseUrl+"/user/powerquery", map);
			log.info(logMsg+"调用统一用户中心返回参数："+info);
		} catch (Exception e) {
			log.error(logMsg+"调用统一用户中心异常",e);
			resultJson.put("returnCode", NewAccountConstant.RETURN_CODE_FAIL);
			resultJson.put("returnMsg", "调用统一用户中心异常");
			return resultJson;
		}
		JSONObject result=JSONObject.parseObject(info);
		JSONObject srvrt=result.getJSONObject("srvrt");//返回接口状态
		if(!"0000".equals(srvrt.getString("resultCode"))){
			log.error(logMsg+"统一用户中心处理失败："+srvrt.getString("resultMessage"));
			resultJson.put("returnCode", NewAccountConstant.RETURN_CODE_FAIL);
			resultJson.put("returnMsg", "统一用户中心处理失败");
			return resultJson;
		}
		JSONObject bizrt=result.getJSONObject("bizrt");//业务返回结果
		JSONArray jsonArray = bizrt.getJSONArray("powerUserList");
		List<Map<String, Object>> powerUserList=new ArrayList<Map<String, Object>>();
		for (int i = 0; i < jsonArray.size(); i++) {
			JSONObject powerUser=jsonArray.getJSONObject(i);
			Map<String, Object> map1=new HashMap<String, Object>();
			map1.put("PROVINCE_CODE", powerUser.getString("provinceId"));//网省编码
			map1.put("ORG_NO", powerUser.getString("powerUnit"));//用电单位编号
			map1.put("POWER_USER_NO",powerUser.getString("powerUserNo"));//用电户号
			powerUserList.add(map1);
		}
		resultJson.put("returnCode", NewAccountConstant.RETURN_CODE_SUCCESS);
		resultJson.put("consNoList", powerUserList);
		return resultJson;
	}
}
